package com.sangfor.ssl.l3vpn.service;

import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/* loaded from: classes2.dex */
public class DTSangforTunnelSocket {
    private static final int DATA_LEN = 128;
    private static final int DEFAULT_TIMEOUT = 300;
    private static final String DEST_IP = "1.1.1.1";
    private static final int DEST_PORT = 1111;
    private static final String SF_ACK_ONLINE = "0";
    private static final String SF_SEND_MAGIC = "SFL3VPN";
    private static final String TAG = "DTSangforTunnelSocket";
    private static final String VPN_QUERY_REQUEST = "sfminiconnect://sslvpnclient/vpn_query?";

    public static boolean detectTunnel(String str) {
        byte[] bArr = new byte[128];
        boolean z = false;
        DatagramSocket datagramSocket = null;
        try {
            try {
                DatagramSocket datagramSocket2 = new DatagramSocket();
                try {
                    byte[] sendMessage = getSendMessage(str);
                    DatagramPacket datagramPacket = new DatagramPacket(sendMessage, sendMessage.length, InetAddress.getByName(DEST_IP), (int) DEST_PORT);
                    DatagramPacket datagramPacket2 = new DatagramPacket(bArr, 128);
                    datagramSocket2.setSoTimeout(300);
                    datagramSocket2.send(datagramPacket);
                    datagramSocket2.receive(datagramPacket2);
                    String str2 = new String(datagramPacket2.getData(), datagramPacket2.getOffset(), datagramPacket2.getLength());
                    if (!TextUtils.isEmpty(str2)) {
                        if (SF_ACK_ONLINE.equals(str2)) {
                            z = true;
                        }
                    }
                    datagramSocket2.close();
                } catch (Exception e) {
                    e = e;
                    datagramSocket = datagramSocket2;
                    Log.error(TAG, "UDP send or receive error", e);
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    return z;
                } catch (Throwable th) {
                    th = th;
                    datagramSocket = datagramSocket2;
                    if (datagramSocket != null) {
                        datagramSocket.close();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
        return z;
    }

    private static byte[] getSendMessage(String str) {
        return String.format("sfminiconnect://sslvpnclient/vpn_query?magic=%s&vpnhost=%s", SF_SEND_MAGIC, str).getBytes();
    }
}
